package com.xm.runtimetask.util;

import android.app.ActivityManager;
import android.app.KeyguardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import java.lang.reflect.Method;
import java.util.List;

public class AppUtils {
    public static final String skjPackName = "com.kuaishou.nebula";
    public static final String skjHomeActivityClassName= "com.yxcorp.gifshow.HomeActivity";

    /**
     * 判断某个界面是否在前台
     *
     * @param context   Context
     * @param className 界面的类名
     * @return 是否在前台显示
     */
    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        if (list != null && list.size() > 0) {
            ComponentName cpn = list.get(0).topActivity;
            if (className.equals(cpn.getClassName()))
                return true;
        }
        return false;
    }

    public static void openApp(Context context, String packageName) {
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(intent);
    }

    /**
     * 重新启动app
     * @param context
     * @param packageName
     */
    public static void resStartAPP(final Context context, final String packageName){
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... params) {
                Log.e("test","====打开快手极速版=准备关闭=");
                //返回键
                try {
                    openApp(context,packageName);//启动快手极速版
                    Thread.sleep(1000);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK
                    Thread.sleep(800);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK
                    Thread.sleep(800);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK
                    Thread.sleep(800);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK、
                    Thread.sleep(800);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK
                    Thread.sleep(800);
                    CmdUtil.execShellCmd("input keyevent 4");//KEYCODE_BACK
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.e("test","====打开快手极速版=打开=");
                openApp(context,packageName);//启动快手极速版
                return true;
            }

            @Override
            protected void onPostExecute(Boolean success) {

            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }


    public static void init(Context context){
        WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            DisplayMetrics outMetrics = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(outMetrics);
            int widthPixels = outMetrics.widthPixels;
            int heightPixels = outMetrics.heightPixels;
            int densityDpi = outMetrics.densityDpi;
            float density = outMetrics.density;
            float scaledDensity = outMetrics.scaledDensity;
            //可用显示大小的绝对宽度（以像素为单位）。
            //可用显示大小的绝对高度（以像素为单位）。
            //屏幕密度表示为每英寸点数。
            //显示器的逻辑密度。
            //显示屏上显示的字体缩放系数。
            Remember.putInt("screenWith",widthPixels);
            Remember.putInt("screenHeight",heightPixels);
        }
    }

    public static int getScreenWith(){
        return Remember.getInt("screenWith",0);
    }
    public static int getScreenHeight(){
        return Remember.getInt("screenHeight",0);
    }

    /**
     * 屏幕是否点亮了
     * @param context
     * @return true亮 flase不亮
     */
    public static boolean isScreenOn(Context context){
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }
    /**
     * 是否锁屏了
     * @param context
     * @return true锁屏 flase未锁屏
     */
    public static boolean isLockDevice(Context context){
        KeyguardManager mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
        return mKeyguardManager.inKeyguardRestrictedInputMode();
    }

}
